<template>
  <div>
    <el-tabs type="border-card">
      <el-tab-pane label="微信配置">
        <r-search
          ref="search"
          :searchData="searchData"
          :searchForm="searchForm"
          :searchHandle="searchHandle"
          :inline="false"
          :searchRules="searchRules"
          labelWidth="150px"
          labelPosition="right"
        >
          <div slot="slot1">
            <el-upload
              class="upload-demo"
              :action="action"
              name="files"
              :limit="1"
              :on-success="on_success1"
              :on-exceed="handleExceed"
            >
              <el-button size="mini" type="primary">点击上传</el-button>
            </el-upload>
          </div>

          <div slot="slot2">
            <el-upload
              class="upload-demo"
              :action="action"
              name="files"
              :limit="1"
              :on-success="on_success2"
              :on-exceed="handleExceed"
            >
              <el-button size="mini" type="primary">点击上传</el-button>
            </el-upload>
          </div>

        </r-search>
      </el-tab-pane>
      <el-tab-pane label="支付宝配置">
        <r-search
          ref="search2"
          :searchData="searchData2"
          :searchForm="searchForm2"
          :searchHandle="searchHandle2"
          :inline="false"
          :searchRules="searchRules2"
          labelWidth="150px"
          labelPosition="right"
        />
      </el-tab-pane>
      <el-tab-pane label="短信配置">

        <r-search
          ref="search3"
          :searchData="searchData3"
          :searchForm="searchForm3"
          :searchHandle="searchHandle3"
          :inline="false"
          :searchRules="searchRules3"
          labelWidth="150px"
          labelPosition="right"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  export default {
    name: "platform_information",
    data(){
      return{
        searchData: {
          wxAppId:'',
          wxMchName:'',
          wxMchId:'',
          wxMchKey:'',
          wxSecrect:'',
          wxCertPath:'',
          serviceId:'',
          msgTemplateId:'',
          wxPemkeyPath:'',
          wxSerialNo:''
        },
        searchForm: [
          { type: "input",label:'微信APPID', placeholder: "", prop: "wxAppId", width: 300 },
          { type: "input",label:'微信商户名称', placeholder: "", prop: "wxMchName", width: 300 },
          { type: "input",label:'微信商户ID', placeholder: "", prop: "wxMchId", width: 300 },
          { type: "input",label:'微信商户KEY', prop: "wxMchKey", width: 300 },
          { type: "input",label:'微信商户密码', placeholder: "", prop: "wxSecrect", width: 300 },
          { type: "input",label:'微信退款证书', placeholder: "",isDisabled:()=>{return true}, prop: "wxCertPath", width: 400 },
          {type:'slot',name:'slot1'},
          { type: "input",label:'支付分ID', placeholder: "", prop: "serviceId", width: 300 },
          { type: "input",label:'公众号消息模板ID', placeholder: "", prop: "msgTemplateId", width: 400 },
          { type: "input",label:'微信私钥证书', placeholder: "",isDisabled:()=>{return true}, prop: "wxPemkeyPath", width: 400 },
          {type:'slot',name:'slot2'},
          { type: "input",label:'商户API证书序列号', placeholder: "", prop: "wxSerialNo", width: 400 },
        ],
        searchHandle: [
          {type:'primary',label:'保存', handle: e => this.elFormSubmit(),}
        ],
        searchRules:{
          wxAppId: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxMchName: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxMchId: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxMchKey: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxSecrect: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxCertPath: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          serviceId: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxPemkeyPath: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          wxSerialNo: [
            {required: true,message: '请输入',trigger: 'blur'},
          ]
        },

        searchData2: {
          aliAppId:'',
          privateKey:'',
          publicKey:''
        },
        searchForm2: [
          { type: "input",label:'支付宝APPID', placeholder: "", prop: "aliAppId", width: 200 },
          // { type: "input",label:'sellerID', placeholder: "", prop: "nickName", width: 200 },
          { type: "textarea",label:'私钥key', placeholder: "", prop: "privateKey", width: 200 },
          { type: "textarea",label:'公钥key', prop: "publicKey", width: 200 },
        ],
        searchHandle2: [
          {type:'primary',label:'保存', handle: e => this.elFormSubmit2(),}
        ],
        searchRules2:{
          aliAppId: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          privateKey: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          publicKey: [
            {required: true,message: '请输入',trigger: 'blur'},
          ]
        },

        searchData3: {
          smsCode:'',
          smsSignName:'',
          smsAccessKeySecret:'',

          smsCode2:'',
          smsCode3:'',
          smsCode4:'',
          msgTemplateId2:'',
          msgTemplateId3:'',
          msgTemplateId4:'',
          msgTemplateId5:'',
          msgTemplateId6:'',
          msgTemplateId7:'',
          msgTemplateId8:'',
          msgTemplateId9:''
        },
        searchForm3: [
          { type: "input",label:'模板ID', placeholder: "", prop: "smsCode", width: 200 },
          { type: "input",label:'短信用户名', placeholder: "", prop: "smsSignName", width: 200 },
          { type: "input",label:'短信密码', placeholder: "", prop: "smsAccessKeySecret", width: 200 },

          { type: "input",label:'余额不足提醒短信模板code', placeholder: "", prop: "smsCode2", width: 200 },
          { type: "input",label:'欠费提醒短信模板code', placeholder: "", prop: "smsCode3", width: 200 },
          { type: "input",label:'充值通知短信模板code', placeholder: "", prop: "smsCode4", width: 200 },
          { type: "input",label:'撤回通知', placeholder: "", prop: "msgTemplateId2", width: 200 },
          { type: "input",label:'退款申请通知', placeholder: "", prop: "msgTemplateId3", width: 200 },
          { type: "input",label:'账户欠费通知', placeholder: "", prop: "msgTemplateId4", width: 200 },
          { type: "input",label:'充值成功通知', placeholder: "", prop: "msgTemplateId5", width: 200 },
          { type: "input",label:'上门取货通知', placeholder: "", prop: "msgTemplateId6", width: 200 },
          { type: "input",label:'余额不足提醒', placeholder: "", prop: "msgTemplateId7", width: 200 },
          { type: "input",label:'处理结果通知', placeholder: "", prop: "msgTemplateId8", width: 200 },
          { type: "input",label:'充值成功通知', placeholder: "", prop: "msgTemplateId9", width: 200 },
        ],
        searchHandle3: [
          {type:'primary',label:'保存', handle: e => this.elFormSubmit3(),}
        ],
        searchRules3:{
          smsCode: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          smsSignName: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          smsAccessKeySecret: [
            {required: true,message: '请输入',trigger: 'blur'},
          ],

          smsCode2:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          smsCode3:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          smsCode4:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId2:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId3:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId4:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId5:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId6:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId7:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId8:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
          msgTemplateId9:[
            {required: true,message: '请输入',trigger: 'blur'},
          ],
        },


        id:''
      }
    },
    computed: {
      action () {
        return process.env.VUE_APP_URL + 'api/file/uploadCert'
      }
    },
    mounted(){
      this.getData();
    },
    methods:{
      on_success1 (res) {
        this.searchData.wxCertPath = res.data.imgPath;
      },
      on_success2 (res) {
        this.searchData.wxPemkeyPath = res.data.imgPath
      },
      handleExceed (files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      async getData(){
        var res=await this.apis.payDetails({});
        this.id=res.data.id;

        var data=res.data;

        for(var key in this.searchData){
          for(var key2 in data){
            if(key==key2){
              this.searchData[key]=data[key2];
            }
          }
        }

        for(var key in this.searchData2){
          for(var key2 in data){
            if(key==key2){
              this.searchData2[key]=data[key2];
            }
          }
        }

        for(var key in this.searchData3){
          for(var key2 in data){
            if(key==key2){
              this.searchData3[key]=data[key2];
            }
          }
        }

      },
      elFormSubmit(){
        this.$refs.search.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.submit1();
          }
        })
      },
      elFormSubmit2(){
        this.$refs.search2.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.submit2();
          }
        })
      },
      elFormSubmit3(){
        this.$refs.search3.$refs.ruleForm.validate((valid) => {
          if (valid) {
            this.submit3();
          }
        })
      },
      async submit1(){
          if(this.id){
            var res=await this.apis.pay_updata({
              id: this.id,
              wxMchName: this.searchData.wxMchName,
              wxMchId: this.searchData.wxMchId,
              wxAppId: this.searchData.wxAppId,
              wxMchKey: this.searchData.wxMchKey,
              wxSecrect: this.searchData.wxSecrect,
              wxCertPath: this.searchData.wxCertPath,
              serviceId: this.searchData.serviceId,
              msgTemplateId: this.searchData.msgTemplateId,
              wxPemkeyPath: this.searchData.wxPemkeyPath,
              wxSerialNo: this.searchData.wxSerialNo,
            });
          }else{
            var res=await this.apis.pay_add({
              wxMchName: this.searchData.wxMchName,
              wxMchId: this.searchData.wxMchId,
              wxAppId: this.searchData.wxAppId,
              wxMchKey: this.searchData.wxMchKey,
              wxSecrect: this.searchData.wxSecrect,
              wxCertPath: this.searchData.wxCertPath,
              serviceId: this.searchData.serviceId,
              msgTemplateId: this.searchData.msgTemplateId,
              wxPemkeyPath: this.searchData.wxPemkeyPath,
              wxSerialNo: this.searchData.wxSerialNo,
            });
          }


          this.$message({
            message:'操作成功',
            type:'success'
          });
      },
      async submit2(){
        if(this.id){
          var res=await this.apis.pay_updata({
            id: this.id,
            aliAppId:this.searchData2.aliAppId,
            privateKey:this.searchData2.privateKey,
            publicKey:this.searchData2.publicKey
          });
        }else{
          var res=await this.apis.pay_add({
            aliAppId:this.searchData2.aliAppId,
            privateKey:this.searchData2.privateKey,
            publicKey:this.searchData2.publicKey
          });
        }

        this.$message({
          message:'操作成功',
          type:'success'
        });
      },
      async submit3(){
        if(this.id){
          var res=await this.apis.pay_updata({
            id: this.id,
            smsCode:this.searchData3.smsCode,
            smsSignName:this.searchData3.smsSignName,
            smsAccessKeySecret:this.searchData3.smsAccessKeySecret,

            smsCode2:this.searchData3.smsCode2,
            smsCode3:this.searchData3.smsCode3,
            smsCode4:this.searchData3.smsCode4,
            msgTemplateId2:this.searchData3.msgTemplateId2,
            msgTemplateId3:this.searchData3.msgTemplateId3,
            msgTemplateId4:this.searchData3.msgTemplateId4,
            msgTemplateId5:this.searchData3.msgTemplateId5,
            msgTemplateId6:this.searchData3.msgTemplateId6,
            msgTemplateId7:this.searchData3.msgTemplateId7,
            msgTemplateId8:this.searchData3.msgTemplateId8,
            msgTemplateId9:this.searchData3.msgTemplateId9
          });
        }else{
          var res=await this.apis.pay_add({
            smsCode:this.searchData3.smsCode,
            smsSignName:this.searchData3.smsSignName,
            smsAccessKeySecret:this.searchData3.smsAccessKeySecret,

            smsCode2:this.searchData3.smsCode2,
            smsCode3:this.searchData3.smsCode3,
            smsCode4:this.searchData3.smsCode4,
            msgTemplateId2:this.searchData3.msgTemplateId2,
            msgTemplateId3:this.searchData3.msgTemplateId3,
            msgTemplateId4:this.searchData3.msgTemplateId4,
            msgTemplateId5:this.searchData3.msgTemplateId5,
            msgTemplateId6:this.searchData3.msgTemplateId6,
            msgTemplateId7:this.searchData3.msgTemplateId7,
            msgTemplateId8:this.searchData3.msgTemplateId8,
            msgTemplateId9:this.searchData3.msgTemplateId9
          });
        }

        this.$message({
          message:'操作成功',
          type:'success'
        });
      }
    }
  }
</script>

<style scoped lang="scss">
  .c1{
    >div{
      >div:nth-child(1){
        line-height: 40px;
        font-weight: bold;
        font-size:18px;
      }
    }
  }
</style>
